/*
 * @Author: xiaosihan 
 * @Date: 2023-08-10 11:24:00 
 * @Last Modified by: xiaosihan
 * @Last Modified time: 2024-12-30 07:29:28
 */

import designEditor from "@/editorComponent/designEditor";
import editorStore from "@/editorComponent/editorStore";
import { Tooltip } from "antd";
import clsx from "clsx";
import { autorun } from "mobx";
import { useEffect, useRef, useState } from "react";
import styles from "./toolRightRotation.module.less";
import rightRotationPNG from "./rightRotation.png";
import rightRotation_hoverPNG from "./rightRotation_hover.png";
import nameNumStore from "@/editorComponent/LeftMenu/NameNum/nameNumStore";


interface Iprops {
}

// 顺时针旋转45°(Alt + G)
export default function ToolRightRotation() {

  const [hover, setHover] = useState(false);
  const dom = useRef<HTMLDivElement | null>(null);
  const [visible, setVisible] = useState(false);
  const [disable, setDisable] = useState(false);

  useEffect(() => autorun(() => {
    const { activeObjectId, lock } = editorStore;
    const { step } = nameNumStore;
    const activeObject = designEditor.fabricCanvas.getActiveObject();
    if (activeObjectId && activeObject && activeObject.type === "i-text") {
      const lockNameNum = (["name", "num"].includes(activeObject.name || "") && step === 3);
      setVisible(true);
      setDisable(lock || lockNameNum);
    } else {
      setVisible(false);
    }
  }), []);

  return (
    <Tooltip
      title="顺时针旋转45°(Alt + G)"
      placement="top"
      mouseLeaveDelay={0}
      mouseEnterDelay={0.5}
    >
      <div
        onClick={() => {
          designEditor.rightRotation45();
        }}
        onMouseEnter={() => {
          setHover(true);
        }}
        onMouseLeave={() => {
          setHover(false);
        }}
        className={clsx(styles.toolRightRotation, visible && styles.visible, disable && styles.disable)}
      >
        <img className={styles.icon} src={hover ? rightRotation_hoverPNG : rightRotationPNG} alt="" />
      </div>
    </Tooltip>
  );

}